#!/bin/bash

# Copyright 2019 Mobvoi Inc. All Rights Reserved.

. ./path.sh || exit 1;

cd ..
stage=5 # start from 0 if you need to start from data preparation
stop_stage=6


cmvn=false
do_delta=false

#dir=/home/work_nfs7/xlgeng/bsmu_template/exp/salmonn_v8_lr5e_5
#train_config=conf/train_salmonn_v7.yaml
dir=
train_config=

data_type=raw # raw
average_checkpoint=false

decode_checkpoint=
decode_checkpoint_name=
# maybe you can try to adjust it if you can not get close results as README.md
average_num=10
# decode_modes="attention_rescoring ctc_greedy_search ctc_prefix_beam_search attention"
decode_modes="salmonn_decode"

added_set_dir="/home/work_nfs4_ssd/xpyan/work_nfs5_ssd/HuaWei_SE_Android/wenet_meeting/data"
# added_sets=("103" "107" "assistant" "huawei_asr/noisy" "huawei_asr/real" "xiaotiancai")
added_sets=( "huawei_asr/noisy" "huawei_asr/real" "107" )
gpu_id=
. tools/parse_options.sh || exit 1;
echo "开始打印主要变量，这些变量有命令行传入"
echo "dir=$dir"
echo "train_config=$train_config"
echo "decode_checkpoint=$decode_checkpoint"
echo "decode_checkpoint_name=$decode_checkpoint_name"
echo "gpu_id=$gpu_id"
echo "stage=$stage"
echo "stop_stage=$stop_stage"
#echo "test_sets=$test_sets"
for test_set in "${added_sets[@]}"; do
{
    echo "prepare test this dataset: $test_set"
}
done
wait

set -e
set -u
set -o pipefail


if [ ${stage} -le 5 ] && [ ${stop_stage} -ge 5 ]; then
  echo 'decode by gengxuelong , start do decode to added set'
  # Test model, please specify the model you want to test by --checkpoint
  cmvn_opts=
  $cmvn && cmvn_opts="--cmvn data/${train_set}/global_cmvn"
  decoding_chunk_size=
  ctc_weight=0.5
  # Polling GPU id begin with index 0
  for test_set in "${added_sets[@]}"; do
  {
    echo "test this dataset: $test_set"
    test_set_name=$(echo "$test_set" | sed 's/\//_/g')
    test_dir=$dir/test_${decode_checkpoint_name}/${test_set_name}
    wer_path=$test_dir/wer
    mkdir -p $test_dir
    export CUDA_VISIBLE_DEVICES=$gpu_id
    python wenet/bin/recognize.py --gpu $gpu_id \
      --mode $decode_modes \
      --config $dir/train.yaml \
      --data_type raw \
      --test_data $added_set_dir/$test_set/data.list \
      --checkpoint $decode_checkpoint \
      --beam_size 10 \
      --batch_size 1 \
      --penalty 0.0 \
      --result_dir $test_dir \
      --ctc_weight $ctc_weight

#    python tools/compute-wer.py --char=1 --v=1 \
#      $added_set_dir/$test_set/text $test_dir/text_hyp > $test_dir/wer
    echo "$test_set has been decoded!"
  }
  done
  wait

fi

if [ ${stage} -le 6 ] && [ ${stop_stage} -ge 6 ]; then
  echo 'compute wer by gengxuelong , start do decode to added set'
  for test_set in "${added_sets[@]}"; do
  {
    echo "compute wer this dataset: $test_set"
    test_set_name=$(echo "$test_set" | sed 's/\//_/g')
    test_dir=$dir/test_${decode_checkpoint_name}/${test_set_name}
    python tools/compute-wer.py --char=1 --v=1 \
      $added_set_dir/$test_set/text $test_dir/text > $test_dir/wer
    echo "$test_set has been decoded!"
  }
  done
  wait

fi
